package config;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ComponentScan("com.neusoft.service")
@MapperScan("com.neusoft.mapper")
@PropertySource(value="classpath:db.properties",encoding="UTF-8")
public class SpringConfiguration {
	@Value("${jdbc.driver}")
	private String driverName;
	@Value("${jdbc.url}")
	private String url;
	@Value("${jdbc.username}")
	private String username;
	@Value("${jdbc.password}")
	private String password;
	
	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource data = new DriverManagerDataSource();
		data.setDriverClassName(driverName);
		data.setUrl(url);
		data.setUsername(username);
		data.setPassword(password);
		return data;
	}
	@Bean
	public SqlSessionFactoryBean sqlSession(DataSource ds){
		SqlSessionFactoryBean s = new SqlSessionFactoryBean();
		s.setDataSource(ds);
		return s;
	}
}
